feat: emit ESM syntax in standalone mode via ajv.code.esm - #872
Open
jaideeppyne wants to merge 1 commit into
Open
feat: emit ESM syntax in standalone mode via ajv.code.esm#872jaideeppyne wants to merge 1 commit into
jaideeppyne wants to merge 1 commit into
Conversation
Standalone mode hardcoded CommonJS require/module.exports, forcing ESM/TypeScript
consumers to add interop shims. Mirror Ajv's standalone code.esm option: when
`{ mode: 'standalone', ajv: { code: { esm: true } } }` is set, emit
import/export default (with explicit .js extensions that ESM resolution requires)
instead of require/module.exports. CommonJS output is unchanged by default.
Closes fastify#679
Signed-off-by: Jaideep Pyne <jaideeppyne1997@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #679.
Standalone mode currently hardcodes CommonJS (
require/module.exports), so ESM and TypeScript consumers have to wrap the generated serializer in an interop layer. This mirrors Ajv's standalonecode.esmoption: when the serializer is built withthe generated code uses
import/export defaultinstead ofrequire/module.exports.Details
options.ajv.code.esm(already threaded through to the validator) so the standalone serializer output matches the validator's module format — the same flag@fastify/ajv-compiler'sStandaloneValidatoralready honors.import Serializer from 'fast-json-stringify/lib/serializer.js'/import Validator from '.../validator.js'andexport default (...)(validator, serializer). Two ESM-specific details are handled:.jsextensions, which ESM resolution requires (CJSrequiredoes not);export default function () {}(...)parses as a function declaration and would drop the invocation.ajv.code.esm === true.Testing
test/standalone-mode.test.jsthat generate ESM output, write it to a.mjsfile, dynamicallyimport()it, and assert it serializes correctly — both without a validator and with anif/then/elseschema that pulls in the Ajv validator dependency.standalone.jsat 100% coverage;eslintclean.